package org.lanqiao.servlet;
import com.alibaba.fastjson.JSON;
import common.Result;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 登录接口
 * 登录的业务逻辑
 * 需求：获取登录的用户名和密码，到数据查询用户信息 返回登录成功或者失败
 * @param
 * @param
 * @throws
 * @throws
 */

@WebServlet("/api/login")
public class LoginServlet extends HttpServlet {
//重写Service方法
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       //获取参数（假定对方传递的数据 username=zhangsan$password =123456）
        //1.编写接口
        req.setCharacterEncoding("utf-8");//请求体
        String username =req.getParameter("username");
        String password =req.getParameter("password");
        //String str =new String(username.getBytes(),"utf-8")强制转码

        //2.处理业务逻辑
        Result result=null;
        if ("admin".equals(username)&&"admin_123456".equals(password)){
            //成功
             result = new Result(200,"登陆成功","lanqiao_token654651151544dhgfhsgfhs");


        }else{
             result = new Result(404,"用户名或密码错误",null);

        }
        //3.

        String json = JSON.toJSONString(result);
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(json);

    }
}
